﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace ToyundaPhone.Converters
{
	public class VisibilityConverter : System.Windows.Data.IValueConverter
	{
		private readonly object[] falseValues;

		public VisibilityConverter()
		{
			falseValues = new object[] { false, (Byte)0, (Int16)0, (Int32)0, (Int64)0, (Single)0, (Double)0, default(DateTime), string.Empty };
		}

		#region IValueConverter Members

		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			System.Windows.Visibility visibility = Visibility.Collapsed;
			if (((value == null) && (parameter != null)) ||
				((value != null) && 
				   ((((value is ValueType) || (value is string)) && 
					   ((!falseValues.Contains(value) && (parameter == null)) ||
						(falseValues.Contains(value) && (parameter != null)))) ||
					(!(value is ValueType) && (parameter == null)))))
			{
				visibility = Visibility.Visible;
			}
			if (value is IEnumerable)
			{
				int count = (value as IEnumerable).OfType<object>().Count();
				if (((count > 0) && (parameter == null)) ||
					((count == 0) && (parameter != null)))
				{
					visibility = Visibility.Visible;
				}
			}
			return visibility;
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}

		#endregion
	}
}
